package cn.tedu.epidemic.webapi.security;

import cn.tedu.community.commons.pojo.user.vo.UserVO;
import cn.tedu.epidemic.webapi.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


@Service
@Slf4j
public class UserDetailServiceImpl implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDuct loadUserByUsername(String s) throws UsernameNotFoundException {
        log.debug("Spring Security调用了loadUserByUsername()方法，参数：{}", s);
        UserVO userVO = userMapper.selectByName(s);
        log.debug("userVO=" + userVO);
        log.debug("从数据库查询用户名【{}】匹配的信息，结果：{}", s, userVO);
        if (userVO == null) {
            return null;//暂时
        }

        List<SimpleGrantedAuthority> authorities = new ArrayList<>();
        SimpleGrantedAuthority authority = new SimpleGrantedAuthority("这是临时使用的一个山寨的权限");
        authorities.add(authority);
        UserDuct userDuct = new UserDuct(
                userVO.getId(),
                userVO.getUsername(),
                userVO.getPassword(),
                true,
                authorities);
        log.debug("userDuct=" + userDuct);
        return userDuct;
    }

}
